#include "StdAfx.h"
#include "CryGameSpyLobby.h"

#if USE_CRYLOBBY_GAMESPY


#define LOBBYUI_PARAM_RICHPRESENCE				0		//-- ptr


CCryGameSpyLobbyUI::CCryGameSpyLobbyUI(CCryLobby* pLobby, CCryLobbyService* pService) : CCryLobbyUI(pLobby, pService)
{
	// Make the CCryLobbyUI base pointers point to our data so we can use the common code in CCryLobbyUI
	for (uint32 i = 0; i < MAX_LOBBYUI_TASKS; i++)
	{
		CCryLobbyUI::m_pTask[i] = &m_task[i];
	}
}

ECryLobbyError CCryGameSpyLobbyUI::Initialise()
{
	ECryLobbyError error = CCryLobbyUI::Initialise();
	return error;
}

ECryLobbyError CCryGameSpyLobbyUI::Terminate()
{
	return CCryLobbyUI::Terminate();
}


ECryLobbyError CCryGameSpyLobbyUI::StartTask(ETask etask, bool startRunning, uint32 user, CryLobbyUITaskID* pUITaskID, CryLobbyTaskID* pLTaskID, CrySessionHandle h, void* pCb, void* pCbArg)
{
	CryLobbyUITaskID tmpUITaskID;
	CryLobbyUITaskID* pUseUITaskID = pUITaskID ? pUITaskID : &tmpUITaskID;
	ECryLobbyError error = CCryLobbyUI::StartTask(etask, startRunning, pUseUITaskID, pLTaskID, h, pCb, pCbArg);
	return error;
}

void CCryGameSpyLobbyUI::StartTaskRunning(CryLobbyUITaskID uiTaskID)
{
	LOBBY_AUTO_LOCK;

	STask* pTask = &m_task[uiTaskID];

	if (pTask->used)
	{
		pTask->running = true;

		switch (pTask->startedTask)
		{
		case eT_ShowGamerCard:
			StartShowGamerCard(uiTaskID);
			break;

		case eT_ShowGameInvite:
			StartShowGameInvite(uiTaskID);
			break;

		case eT_ShowFriends:
			StartShowFriends(uiTaskID);
			break;

		case eT_ShowFriendRequest:
			StartShowFriendRequest(uiTaskID);
			break;

		case eT_SetRichPresence:
			StartSetRichPresence(uiTaskID);
			break;
		}
	}
}

void CCryGameSpyLobbyUI::EndTask(CryLobbyUITaskID uiTaskID)
{
	LOBBY_AUTO_LOCK;

	STask* pTask = &m_task[uiTaskID];

	if (pTask->used)
	{
		if (pTask->pCb)
		{
			switch (pTask->startedTask)
			{
			case eT_ShowGamerCard:
			case eT_ShowGameInvite:
			case eT_ShowFriends:
			case eT_ShowFriendRequest:
			case eT_SetRichPresence:
				((CryLobbyUICallback)pTask->pCb)(pTask->lTaskID, pTask->error, pTask->pCbArg);
				break;
			}
		}

		if (pTask->error != eCLE_Success)
		{
			NetLog("[Lobby] LobbyUI EndTask %d (%d) Result %d", pTask->startedTask, pTask->subTask, pTask->error);
		}

		FreeTask(uiTaskID);
	}
}

void CCryGameSpyLobbyUI::StopTaskRunning(CryLobbyUITaskID uiTaskID)
{
	STask* pTask = &m_task[uiTaskID];

	if (pTask->used)
	{
		pTask->running = false;
		TO_GAME_FROM_LOBBY(&CCryGameSpyLobbyUI::EndTask, this, uiTaskID);
	}
}

ECryLobbyError CCryGameSpyLobbyUI::ShowGamerCard(uint32 user, CryUserID userID, CryLobbyTaskID* pTaskID, CryLobbyUICallback cb, void* pCbArg)
{
	LOBBY_AUTO_LOCK;

	ECryLobbyError error = eCLE_InvalidRequest;
	return error;
}

void CCryGameSpyLobbyUI::StartShowGamerCard(CryLobbyUITaskID uiTaskID)
{
	UpdateTaskError(uiTaskID, eCLE_InvalidRequest);
	StopTaskRunning(uiTaskID);
}

ECryLobbyError CCryGameSpyLobbyUI::ShowGameInvite(uint32 user, CrySessionHandle h, CryUserID* pUserIDs, uint32 numUserIDs, CryLobbyTaskID* pTaskID, CryLobbyUICallback cb, void* pCbArg)
{
	LOBBY_AUTO_LOCK;

	ECryLobbyError error = eCLE_InvalidRequest;
	return error;
}

void CCryGameSpyLobbyUI::StartShowGameInvite(CryLobbyUITaskID uiTaskID)
{
	UpdateTaskError(uiTaskID, eCLE_InvalidRequest);
	StopTaskRunning(uiTaskID);
}

ECryLobbyError CCryGameSpyLobbyUI::ShowFriends(uint32 user, CryLobbyTaskID* pTaskID, CryLobbyUICallback cb, void* pCbArg)
{
	LOBBY_AUTO_LOCK;

	ECryLobbyError error = eCLE_InvalidRequest;
	return error;
}

void CCryGameSpyLobbyUI::StartShowFriends(CryLobbyUITaskID uiTaskID)
{
	UpdateTaskError(uiTaskID, eCLE_InvalidRequest);
	StopTaskRunning(uiTaskID);
}

ECryLobbyError CCryGameSpyLobbyUI::ShowFriendRequest(uint32 user, CryUserID userID, CryLobbyTaskID* pTaskID, CryLobbyUICallback cb, void* pCbArg)
{
	LOBBY_AUTO_LOCK;

	ECryLobbyError error = eCLE_InvalidRequest;
	return error;
}

void CCryGameSpyLobbyUI::StartShowFriendRequest(CryLobbyUITaskID uiTaskID)
{
	UpdateTaskError(uiTaskID, eCLE_InvalidRequest);
	StopTaskRunning(uiTaskID);
}


ECryLobbyError CCryGameSpyLobbyUI::SetRichPresence(uint32 user, SCryLobbyUserData* pData, uint32 numData, CryLobbyTaskID* pTaskID, CryLobbyUICallback cb, void* pCbArg)
{
	LOBBY_AUTO_LOCK;

	ECryLobbyError error = eCLE_Success;

	CryLobbyUITaskID uiTaskID;

	if ((numData > 0) && pData)
	{
		error = StartTask(eT_SetRichPresence, false, user, &uiTaskID, pTaskID, CrySessionInvalidHandle, (void*)cb, pCbArg);

		if (error == eCLE_Success)
		{
			STask* pTask = &m_task[uiTaskID];

			error = CreateTaskParamMem(uiTaskID, LOBBYUI_PARAM_RICHPRESENCE, 0, GP_STATUS_STRING_LEN);
			if (error == eCLE_Success)
			{
				char* pStatusString = (char *)m_pLobby->MemGetPtr(pTask->paramsMem[LOBBYUI_PARAM_RICHPRESENCE]);
				memset(pStatusString, 0, GP_STATUS_STRING_LEN);
				
				SCryLobbyPresenceConverter convert;
				convert.m_pData = pData;
				convert.m_numData = numData;
				convert.m_pStringBuffer = (uint8*)pStatusString;
				convert.m_sizeOfStringBuffer = GP_STATUS_STRING_LEN;
				convert.m_sessionId = CrySessionInvalidID;

				SConfigurationParams presenceItem = {CLCC_CRYLOBBY_PRESENCE_CONVERTER, {NULL}};
				presenceItem.m_pData = &convert;

				m_pLobby->GetConfigurationInformation(&presenceItem, 1);
		
				if ((convert.m_sizeOfStringBuffer > 0) && (convert.m_sizeOfStringBuffer < GP_STATUS_STRING_LEN))
				{
					pStatusString[GP_STATUS_STRING_LEN-1] = 0;
	
					FROM_GAME_TO_LOBBY(&CCryGameSpyLobbyUI::StartTaskRunning, this, uiTaskID);
				}
				else
				{
					error = eCLE_InvalidRequest;
					FreeTask(uiTaskID);
				}
			}
			else
			{
				FreeTask(uiTaskID);
			}
		}
	}
	else
	{
		error = eCLE_InvalidParam;
	}

	return error;
}


void CCryGameSpyLobbyUI::StartSetRichPresence(CryLobbyUITaskID uiTaskID)
{
	STask* pTask = &m_task[uiTaskID];

	if (pTask->paramsMem[LOBBYUI_PARAM_RICHPRESENCE] != TMemInvalidHdl)
	{
		const char* pStatusString = (const char *)m_pLobby->MemGetPtr(pTask->paramsMem[LOBBYUI_PARAM_RICHPRESENCE]);

		if (m_pService)
		{
			CCryGameSpyLobbyService* pService = (CCryGameSpyLobbyService*)m_pService;
			if (pService->HaveGPConnection())
			{
				pService->GPSetStatusString( pStatusString );
			}
			else
			{
				UpdateTaskError(uiTaskID, eCLE_InvalidParam);
			}
		}
		else
		{
			UpdateTaskError(uiTaskID, eCLE_InvalidParam);
		}
	}
	else
	{
		UpdateTaskError(uiTaskID, eCLE_OutOfMemory);
	}

	StopTaskRunning(uiTaskID);
}


#endif // USE_CRYLOBBY_GAMESPY
